public with sharing class AssetManager {
	
	private String ASSET_STATUS_PURCHASED = 'Purchased';
	
	/**
	* <P> This method is used to populate the Asset for an Opportunity.</P>
	* @param: Opportunity
	* @return List of Assets
	*/
	private Asset populateAssetForOpportunityLineItems(OpportunityLineItem opportunityLineItems){
		
			Asset asset= null;
				
			if(null!=opportunityLineItems ){
					 asset = new Asset();
					asset.AccountId = opportunityLineItems.Opportunity.AccountId;
					asset.PurchaseDate =opportunityLineItems.Opportunity.CloseDate ;
					asset.Status = ASSET_STATUS_PURCHASED;
					
					
					asset.Description =opportunityLineItems.Description;
					asset.InstallDate = opportunityLineItems.ServiceDate;
					asset.Price = opportunityLineItems.TotalPrice;
					asset.Quantity = opportunityLineItems.Quantity;
					
					
					asset.Name = opportunityLineItems.PricebookEntry.Product2.Name;
					asset.Product2Id = opportunityLineItems.PricebookEntry.Product2Id;
					asset.SerialNumber = opportunityLineItems.PricebookEntry.ProductCode;
					
		   }
		
  		return asset;
	}
	
	/**
	* <P> This method is used to Create Assets for the given Opportunities.</P>
	* @param: Opportunity
	* @return List of Database.Saveresult 
	*/
	public List<Database.Saveresult>  createAssetsForOpportunityLineItems(List<OpportunityLineItem> opportunityLineItems){
		List<Asset> assets= null;
		List<Database.Saveresult> saveResult=null;
		
		if(null!= opportunityLineItems && !opportunityLineItems.isEmpty()){
			assets = new List<Asset>();
			for (OpportunityLineItem oli : opportunityLineItems){
				
				Asset asset=populateAssetForOpportunityLineItems(oli);
				assets.add(asset);
				//insertAssets(asset);
				//oli.Asset__c=asset.Id;
				//System.debug('Stting Asset id:= ' + asset.Id + ' into OLI:= ' + oli.id);
			}
			if(!assets.isEmpty()){
				
				saveResult= insertAssetsWithResult(assets);
			
			}
		}
		
		
		return saveResult;
		
	}
	
	public List<Database.Deleteresult> deleteAssetsForOpportunities(set<Id> opportunitiesIds){

		List<Database.Deleteresult> result=null;
		
		if(null!= opportunitiesIds && !opportunitiesIds.isEmpty()){
			
			List<Asset> assetsForOLI = getAssetsForOpportunities(opportunitiesIds);
			
			if(null!=assetsForOLI && !assetsForOLI.isEmpty() ){
				
				result = deleteAssetsWithResult(assetsForOLI);
				
			}
			
		}
		return result;
		
	}
	
		
	/**
	* <P> This method is used to get the list of Assets for Opportunities.</P>
	* @param: id's of Opportunity
	* @return List of Assets
	*/
	public List<Asset> getAssetsForOpportunities(set<Id> opportunitiesIds){
		
		List<Asset> assetsForOLIs= [SELECT id FROM Asset where ID in (SELECT OLI.ASSET__c from OpportunityLineItem OLI  WHERE  oli.OpportunityId in :opportunitiesIds)];
		
		return assetsForOLIs;
		 
	}
	
	/**
	* <P> This method is used to Create the Assets in The Database.</P>
	* @param: assets as List of Assets 
	* @retun Database.Saveresult
	*/
	public List<Database.Saveresult> insertAssetsWithResult(List<Asset> assets){
		
		return Database.insert(assets,true);
		
		
	}
	
	
	
	/**
	* <P> This method is used to Delete the Assets from The Database.</P>
	* @param: assets as List of Assets 
	* @return Database.Deleteresult
	*/
	public List<Database.Deleteresult> deleteAssetsWithResult(List<Asset> assets){
		
		 return Database.delete(assets,true);
	}
	
	
		/**
	* <P> This method is used to Create the Assets in The Database.</P>
	* @param: assets as List of Assets 
	* 
	*/
	public void insertAssets(List<Asset> assets){
		
		insert assets;
	}
	
	
		/**
	* <P> This method is used to Create the Asset in The Database.</P>
	* @param: assets as List of Assets 
	* 
	*/
	public void insertAssets(Asset assets){
		
		insert assets;
	}
	
	
	/**
	* <P> This method is used to Delete the Assets from The Database.</P>
	* @param: assets as List of Assets 
	* 
	*/
	public void deleteAssets(List<Asset> assets){
		
		delete assets;
	}
	
	
}